const util = require('../../utils/util.js')

const app = getApp()
Page({
  data: {
    date: util.formatMonth(new Date()),
    openId: app.globalData.openId,
    selectAccountIndex:0,
    role:"normal",
    order:{},
    accountArray: [
     
    ],
    monthOrders:[
      
    ]
  },
  onReady:function(options){
    const setData = this.setDataPro;
    wx.request({
      url: app.globalData.baseUrl +'account/queryAllAccounts',
      method: "GET",
      success: function (res) {
        if (res.data.code == 0) {
          console.log(res.data);
          const obj = new Object;
          obj.accountArray = res.data.data;
          setData(obj);
        }
      }
    });
  },
  onShow: function (options) {
    const setData = this.setDataPro;
    const date = this.data.date;
    const selectOpenId = app.globalData.openId;
    wx.request({
      url: app.globalData.baseUrl +'order/queryMonthOrders?date=' + date + "&openId=" + selectOpenId,
      method: "GET",
      success: function (res) {
        if (res.data.code == 0) {
          console.log(res.data);
          const orders=res.data.data;
          const obj=new Object;
          obj.order = orders;
          setData(obj);
        }
      }
    });
  },
  setDataPro: function (obj) {
    this.setData(obj);
  },
  navigateToOrderShow: function (event) {
    const orderType = event.currentTarget.dataset.type;
    const itemData = event.currentTarget.dataset.item;
    const params = JSON.stringify(itemData);
    if (orderType == 0) {
      wx.navigateTo({
        url: '/pages/transportOrderShow/transportOrderShow?order=' + params,
      })
    } else {
      wx.navigateTo({
        url: '/pages/repairOrderShow/repairOrderShow?order=' + params,
      })
    }
  },
  bindDateChange: function (e) {
    const setData = this.setDataPro;
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    });
    const selectDate = e.detail.value;
    const selectOpenId = this.data.accountArray[this.data.selectAccountIndex].openId;
    wx.request({
      url: app.globalData.baseUrl +'order/queryMonthOrders?date=' + selectDate + "&openId=" + selectOpenId,
      method: "GET",
      success: function (res) {
        if (res.data.code == 0) {
          console.log(res.data);
          const orders = res.data.data;
          const obj = new Object;
          obj.order = orders;
          setData(obj);
        }
      }
    });
  },
  bindAccountChange:function(e){
    const setData = this.setDataPro;
    const selectOpenId = this.data.accountArray[e.detail.value].openId;
    this.setData({
      selectAccountIndex: e.detail.value,
      openId: selectOpenId
    });
    wx.request({
      url: app.globalData.baseUrl +'order/queryMonthOrders?date=' + this.data.date + "&openId=" + selectOpenId,
      method: "GET",
      success: function (res) {
        if (res.data.code == 0) {
          console.log(res.data);
          const orders = res.data.data;
          const obj = new Object;
          obj.order = orders;
          setData(obj);
        }
      }
    });
  }
});